package com.loveprogrammer.netty.simple.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

/**
 * @ClassName AppServerHello
 * @Description TODO
 * @Author admin
 * @Date 2023/11/15 15:24
 * @Version 1.0
 */
public class AppServerHello {
    private int port;

    public AppServerHello(int port)
    {
        this.port = port;
    }

    public void run() throws Exception{
        // Netty的Reactor线程池，初始化了一个NioEventLoop数组，用来处理I/O操作,如接受新的连接和读/写数据
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            ServerBootstrap sbt = new ServerBootstrap();

            sbt.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        //ChannelInitializer是一个特殊的处理类，他的目的是帮助使用者配置一个新的Channel,用于把许多自定义的处理类增加到pipline上来
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //配置childHandler来通知一个关于消息处理的InfoServerHandler实例
                            socketChannel.pipeline().addLast(new HandlerServerHello());
                        }
                    });

            ChannelFuture channelFuture = sbt.bind().sync();
            System.out.println("在"+ channelFuture.channel().localAddress()+"上开启监听");

            //阻塞操作，closeFuture()开启了一个channel的监听器（这期间channel在进行各项工作），直到链路断开
            channelFuture.channel().closeFuture().sync();

        }finally {
            // 关闭EventLoopGroup并释放所有资源，包括所有创建的线程
            group.shutdownGracefully().sync();
        }


    }

    public static void main(String[] args)  throws Exception
    {
        new AppServerHello(18080).run();
    }
}
